var baseURL = 'https://trial.beelink.com:10025';
var authorities = "";
var user_data = "";
var user_menu = "";
accessid = 'LTAI4GHNftFDbymfwaB6ii5m';
accesskey = 'PxOl63eA3OccR12OMZggJYM338niAK';
host = "https://resource.beelink.com/";

$(function () {
    if ($.cookie('user_data') && $.cookie('authorities')) {
        authorities = $.cookie('authorities');
        $.ajax({
            url: baseURL + '/webapi/loading/loading',
            type: 'post',
            dataType: 'json',
            async: false,//同步
            headers: {'authorities': authorities},
            data: {
                "token": authorities
            },
            success: function (data) {
                if (data.data.webUserInfo != null) {
                    user_data = JSON.parse($.cookie('user_data'));
                    console.log(user_data);
                } else {
                    console.log(1);
                    // alert("请先登录");
                    // window.location.href = "login.html";
                }
            }
        })

    } else {
        console.log(2);
        // alert("请先登录");
        // window.location.href = "login.html";

    }


//退出登录start
    $("#logout").on('click', function () {
        $.session.clear();
        $.removeCookie('user_data', {path: '/'})
        $.removeCookie('authorities', {path: '/'})
        window.location.href = "login.html";
    });
//退出登录end
    $(".logo_text").click(function () {
        location.href = "index.html"
    });
//文章详情页面跳转
    $(".container").on('click', '.link', function () {
        let uid = $(this).attr('uid')
        let type = $(this).attr('type');
        $.session.set('uid', uid)
        if (type == 'svideo' || type == 'lvideo') {
            window.location.href = "video.html?uid="+uid
        } else {
            window.location.href = "news.html?uid="+uid
        }
    });
//用户跳转
    $(".container,.zuopinguanli,.pinglunguanli").on('click', '.user_link', function () {
        let user_id = $(this).attr('user_id');
        $.session.set('user_id',user_id)
        window.location.href = "user.html?user_id="+user_id
    });


})


//公共函数区域
var $_GET = (function () {
    var url = window.location.href.toString();
    var u = url.split("?");
    if (typeof (u[1]) == "string") {
        u = u[1].split("&");
        var get = {};
        for (var i in u) {
            var j = u[i].split("=");
            get[j[0]] = j[1];
        }
        return get;
    } else {
        return {};
    }
})();

// type channel/special/work/template/website/app/ad/ user/${user.id}/
function getAliossPath(type, fileName) {
    if (fileName == null) {
        return "images/index/视角LOGO 背景.jpg"
    } else {
        return "https://resource.beelink.com/ydupf/" + type + "/" + fileName;
    }
}

// 从一个图像url列表中提取第一张图像;pics 图像url地址，多个用逗号分隔;width 获取的图像宽度;timestamp 时间戳，0则不参与
function getPicUrl(pics, width, timestamp) {
    if (pics != null && pics.length > 0) {
        var ss = pics.split(",");
        var param = "";
        if (width > 0) {
            param += "x-oss-process=image/resize,w_" + width;
        }
        if (timestamp > 0) {
            if (param != "") param += "&";
            param += "t=" + timestamp;
        }
        if (param != "") {
            return ss[0] + "?" + param;
        } else {
            return ss[0];
        }
    }
    return "";
}

// 将文件存储到alioss服务器上去
// function toalioss(dirName, formData, fileName,) {
//
//     //验证文本域
//     var policyText =
//         {
//             "expiration": "2100-01-01T12:00:00.000Z", //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
//             "conditions": [
//                 ["content-length-range", 0, 1048576000] // 设置上传文件的大小限制
//             ]
//         };
//     var policyBase64 = Base64.encode(JSON.stringify(policyText))
//     message = policyBase64
//     var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, {asBytes: true});
//     var signature = Crypto.util.bytesToBase64(bytes);
//
//     $.ajax({
//         url: host,
//         type: 'POST',
//         dataType: 'json',
//         async: false,//同步
//         cache: false,
//         contentType: false,
//         processData: false,
//         data: {
//             'Filename': fileName,
//             'key': "ydupf/" + dirName + "/" + fileName,
//             'policy': policyBase64,
//             'OSSAccessKeyId': accessid, //accessKeyId,
//             'success_action_status': '200', //让服务端返回200，不然，默认会返回204
//             'signature': signature,
//             'file': formData,
//         },
//         success: function (data) {
//             console.log(data)
//         }
//     })
// }


// getSuffix() 方法用于获取文件名的后缀
var getSuffix = function (fileName) {
    var pos = fileName.lastIndexOf(".");
    var suffix = '';
    if (pos != -1) {
        suffix = fileName.substring(pos);
    }
    return suffix;
};

